<?php
	//引入配置文件和数据库操作函数
	require '../../public/config.php';
	require '../../public/mysql.php';
	require '../../public/function.php';
	//从数据库中查询出用户的数据
	$link = connect();
	//查询出分类数据
	//准备sql和执行sql
	$sql = "select * from category";
	if ($link['error']) {
		$result = query($link['mess'], $sql);
	}	
	$cates= getTree($result, 0);

	//需要生成一个查询的条件
	$where='';
	$where1='';
	if (!empty($_GET['name'])) {
		$search=$_GET['name'];
		$where.= " and name like '%{$search}%'";
		$where1.= " and g.name like '%{$search}%'";
	}


//	分页
	$page = empty($_GET['p']) ? 1 :$_GET['p'];

	//设置每页显示几条数据
	$num = 20;
	//查询数据的总条数
	$sql = "select count(*) total from goods where 1 $where";
	$result = query($link['mess'], $sql);
	$totalNum = $result[0]['total'];

	//计算总页数
	$totalPage = ceil($totalNum / $num);

	//判断页面是否合法
	if ($page < 1) {
		$page = 1;
	}
	if ($page > $totalPage) {
		$page =  $totalPage;
	}
	
	//计算出起始位置
	$start = ($page - 1) * $num;
	//查询用户信息
	//准备sql语句
	$sql="select g.*,c.name cname from goods g,category c where g.cid = c.id $where1 limit $start,$num";

	$goods=query($link['mess'],$sql);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>s54管理中心 - 商品列表 </title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link href="../include/Css/general.css" rel="stylesheet" type="text/css" />
<link href="../include/Css/main.css" rel="stylesheet" type="text/css" />
</head>
<body>
<h1>
    <span class="action-span"><a href="./goodsAdd.php">添加新商品</a></span>
    <span class="action-span1"><a href="__GROUP__">ECSHOP 管理中心</a></span>
    <span id="search_id" class="action-span1"> - 商品列表 </span>
    <div style="clear:both"></div>
</h1>
<div class="form-div">
    <form action="./goodsList.php" name="searchForm" method='get'>
        <img src="../include/Images/icon_search.gif" width="26" height="22" border="0" alt="search" />
 
        <!-- 关键字 -->
		
        关键字:<input type="text" name="name" size="15" />
        <input type="submit" value=" 搜索 " class="button" />
    </form>
</div>

<!-- 商品列表 -->
<form method="post" action="" name="listForm" onsubmit="">
    <div class="list-div" id="listDiv">
        <table cellpadding="3" cellspacing="1">
            <tr>
                <th>编号</th>
                <th>商品名称</th>
                <th>商品分类</th>
                <th>商品图片</th>
                <th>价格</th>
                <th>上架</th>
                <th>热销</th>
                <th>新品</th>
                <th>出产地</th>
                <th>库存</th>
                <th>添加时间</th>
                <th>操作</th>
            </tr>
			<?php if (!empty($goods)) :?>
            <?php foreach ($goods as $val) :?>
            <tr>
                <td align="center"><?php echo $val['id']?></td>
                <td align="center" class="first-cell"><span><?php echo $val['name'];?></span></td>
                <td align="center" class="first-cell"><span><?php echo $val['cname'];?></span></td>
				<?php
					//拼接图片的路径
					$path = '../../public/upload/'.$val['pic'];
				?>
                <td align="center"><img width="50" src="<?php echo $path;?>"></td>
                <td align="center"><span><?php echo $val['price']?></span></td>
                <td align="center"><?php echo $val['is_sale'] == 0 ? 'No' : 'Yes';?></td>
                <td align="center"><?php echo $val['is_hot'] == 0 ? 'No' : 'Yes';?></td>
                <td align="center"><?php echo $val['is_new'] == 0 ? 'No' : 'Yes';?></td>
                <td align="center"><?php echo $val['company']?></td>
                <td align="center"><span><?php echo $val['store']?></span></td>
                <td align="center"><span><?php echo date('Y-m-d H:i:s', $val['addtime'])?></span></td>
                <td align="center">
                <a href="./imageList.php?id=<?php echo $val['id']?>" target="" title="图片管理"><img src="../include/Images/icon_view.gif" width="16" height="16" border="0" /></a>
                <a href="./goodsEdit.php?id=<?php echo $val['id']?>" title="编辑"><img src="../include/Images/icon_edit.gif" width="16" height="16" border="0" /></a>
                <a href="./action.php?a=del&id=<?php echo $val['id']?>" onclick="" title="删除"><img src="../include/Images/icon_trash.gif" width="16" height="16" border="0" /></a></td>
            </tr>
            <?php endforeach;?>
			<?php else:?>
			<tr>
				<td colspan="12">暂无信息</td>
			</tr>
			<?php endif;?>
        </table>

    <!-- 分页开始 -->
    <table id="page-table" cellspacing="0">
            <tr>
                <td width="80%">&nbsp;</td>
                <td align="center" nowrap="true">
				   <span>共<?php echo $totalPage?>页</span>
				   <span>第<?php echo $page?>/<?php echo $totalPage?>页</span>
                    <a href="goodsList.php?p=1">首页</a>
					<a href="goodsList.php?p=<?php echo $page - 1?>">上一页</a>
					<a href="goodsList.php?p=<?php echo $page + 1?>">下一页</a>
					<a href="goodsList.php?p=<?php echo $totalPage?>">尾页</a>
                </td>
            </tr>
        </table>
    <!-- 分页结束 -->
    </div>
</form>

<div id="footer">
共执行 7 个查询，用时 0.028849 秒，Gzip 已禁用，内存占用 3.219 MB<br />
版权所有 &copy; 2005-2012 暴走少年哈尔，并保留所有权利。</div>

</body>
</html>
